package com.weitu.contractor.service.impl;

import com.weitu.contractor.entity.SafeProductCert;
import com.weitu.contractor.entity.SafeProductCertCriteria;
import com.weitu.contractor.mapper.SafeProductCertMapper;
import com.weitu.contractor.service.SafeProductCertService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SafeProductCertServiceImpl implements SafeProductCertService {
    private final SafeProductCertMapper safeProductCertMapper;

    private static final Logger logger = LoggerFactory.getLogger(SafeProductCertServiceImpl.class);

    @Autowired
    public SafeProductCertServiceImpl(SafeProductCertMapper safeProductCertMapper) {
        this.safeProductCertMapper = safeProductCertMapper;
    }

    @Override
    public int countByCriteria(SafeProductCertCriteria example) {
        int count = this.safeProductCertMapper.countByCriteria(example);
        logger.debug("count: {}", count);
        return count;
    }

    @Override
    public SafeProductCert selectByPrimaryKey(String id) {
        return this.safeProductCertMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SafeProductCert> selectByCriteria(SafeProductCertCriteria example) {
        return this.safeProductCertMapper.selectByCriteria(example);
    }

    @Override
    @Transactional
    public int deleteByPrimaryKey(String id) {
        return this.safeProductCertMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int updateByPrimaryKeySelective(SafeProductCert record) {
        return this.safeProductCertMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional
    public int updateByCriteriaSelective(SafeProductCert record, SafeProductCertCriteria example) {
        return this.safeProductCertMapper.updateByCriteriaSelective(record, example);
    }

    @Override
    @Transactional
    public int insertSelective(SafeProductCert record) {
        return this.safeProductCertMapper.insertSelective(record);
    }

    @Override
    public boolean checkIfCertCodeExist(String contractorId, String certCode) {
        SafeProductCertCriteria criteria = new SafeProductCertCriteria();
        criteria.createCriteria().andCertCodeEqualTo(certCode).andDelStateEqualTo(0).andContractorIdEqualTo(contractorId);
        return safeProductCertMapper.countByCriteria(criteria) > 0;
    }

    @Override
    public boolean checkIfExtraSafeCertExist(String contractorId, String id) {
        SafeProductCertCriteria criteria = new SafeProductCertCriteria();
        criteria.createCriteria().andNameEqualTo("安全生产许可证").andDelStateEqualTo(0).andContractorIdEqualTo(contractorId).andIdNotEqualTo(id);
        return safeProductCertMapper.countByCriteria(criteria) > 0;
    }

    @Override
    public List<SafeProductCert> selectByContractorId(String contractorId) {
        SafeProductCertCriteria criteria = new SafeProductCertCriteria();
        criteria.createCriteria()
                .andDelStateEqualTo(0)
                .andContractorIdEqualTo(contractorId)
                .andNameEqualTo("安全生产许可证");
        return safeProductCertMapper.selectByCriteria(criteria);
    }

    @Override
    public List<String> selectContractorIdsByValidEndDateGreaterThanOrEqualTo(String minDate) {
        List<String> contractorIds = new ArrayList<String>();
        SafeProductCertCriteria cr = new SafeProductCertCriteria();
        cr.createCriteria().andValidEndDateGreaterThanOrEqualTo(minDate);
        List<SafeProductCert> safeProductCerts = this.selectByCriteria(cr);
        if (safeProductCerts.isEmpty()) {
            return contractorIds;
        }
        for (SafeProductCert safeProductCert : safeProductCerts) {
            contractorIds.add(safeProductCert.getContractorId());
        }
        return contractorIds;
    }

    @Override
    public List<String> selectContractorIdsByValidEndDateLessThanOrEqualTo(String maxDate) {
        List<String> contractorIds = new ArrayList<String>();
        SafeProductCertCriteria cr = new SafeProductCertCriteria();
        cr.createCriteria().andValidEndDateLessThanOrEqualTo(maxDate);
        List<SafeProductCert> safeProductCerts = this.selectByCriteria(cr);
        if (safeProductCerts.isEmpty()) {
            return contractorIds;
        }
        for (SafeProductCert safeProductCert : safeProductCerts) {
            contractorIds.add(safeProductCert.getContractorId());
        }
        return contractorIds;
    }
}